import { requestClient, BodyRequestClient } from '#/api/request';

export namespace ClientListApi {
  export interface getClientListResult {
    data: string;
    status: number;
  }
}
export namespace TableApi {
  export interface PageFetchParams {
    [key: string]: any;
    page: number;
    pageSize: number;
  }
}
export namespace SampleApi {
  export interface receiveSampleResult {
    sampleNo: string[]; // 定义为数组
    receiveStatus: boolean;
    sampleStatus?: string;
    receiveDate?: Date;
    receiver?: string;
  }
  export interface updateParams {
    action: 'update' | 'delete' | 'insert';
    data: any;
  }
  export interface testDataParams {
    sampleNo: string;
    sampleName: string;
    data: any;
  }
  export interface downloadParams {
    type: 'sample' | 'report';
    sampleNoArray: string | string[];
    outputMode: 'OnlyDocx' | 'OnlyPdf' | 'All'
  }
}

/**
 * 获取委托单位
 */
export async function getClientListApi() {
  return requestClient.post<ClientListApi.getClientListResult>('/apps/list', {
    Listtype: '1',
  });
}

/**
 * 分页查询委托单数据
 */
export async function getOrderListApi(params: TableApi.PageFetchParams) {
  return requestClient.post('/apps/list', {
    ...params,
    Listtype: '2',
  });
}

/**
 * 处理委托单数据，主要用于提交数据和获取数据；
 * 提交数据时，必须要有orderNo
 * 更新数据时，必须要有orderNo和data
 * 获取信息时，当orderNo为空或null时，data为"委托日期(YYYY-MM-DD)"时查询对应月份下的最大的MSDS编号，否则返回所有MSDS数据；
 * 当orderNo不为空时，返回对应编号的数据
 * @param {string} action 处理类型(submit:提交, getInfo:获取信息, update:更新)
 * @param {string} orderNo MSDS编号(action为submit、update时必须要有, getInfo时：非空则获取对应的MSDS信息)
 * @param {any} data 数据(submit、update:待提交的MSDS数据, getInfo:为"委托日期"返回最大的MSDS编号、其他值则返回MSDS数据)
 */
export async function handleOrderdataApi(action: 'submit' | 'getInfo' | 'update', orderNo?: string, data?: any) {
  return requestClient.post('apps/data', {
    action: action,
    orderNo: orderNo,
    data: data,
  });
}

/**
 * 收样并创建新的样品台账记录
 * @param {SampleApi.newSampleResult} data 包含sampleNo、receiveStatus、sampleStatus、receiveDate、receiver
 */
export async function receiveSampleApi(data: SampleApi.receiveSampleResult) {
  return requestClient.post('apps/receiveSample', data);
}

/**
 * 获取报告列表
 *
 */
export async function getReportListApi(params: TableApi.PageFetchParams) {
  return requestClient.post('/apps/report', {
    ...params,
    Listtype: '1',
  });
}

export async function printReportApi(data: any) {
  return requestClient.post('/apps/report', { Listtype: '2', data: data }, { responseType: 'blob' });
}

/*==============================================================================================================*/
/**
 * 分页查询样品台账数据
 */
export async function getSampleListApi(params: TableApi.PageFetchParams) {
  return requestClient.post('/apps/sampleList', params);
}

/**
 * 查询样品数据
 * @param {string} sampleNo 样品编号
 */
export async function getSampleDataApi(sampleNo: string) {
  return requestClient.get('/apps/getSampleData', { params: { sampleNo } });
}

/**
 * 更新样品台账数据
 * @param {SampleApi.updateParams} data 包含action和data
 * @param action update、delete、insert
 *
 */
export async function updateSampleDataApi(params: SampleApi.updateParams) {
  return requestClient.post('/apps/updateSampleData', params);
}

/**
 * 更新测试数据
 * @param {SampleApi.testDataParams} params 包含sampleNo和data
 */
export async function updateReportdataApi(params: SampleApi.testDataParams) {
  return requestClient.post('/apps/updateReportdata', params);
}

/**
 * 下载测试报告/委托单
 * @param type sample:委托单 report:测试报告
 * @param sampleNoArray 样品编号数组
 */
export async function downloadTestFilesApi(params: SampleApi.downloadParams) {
  return requestClient.post('/apps/downloadFiles', params);
}
